<?php
namespace app\app\controller\v2;
use app\app\Appbase;

class GoodsSeckill extends Appbase 
{
	
	/**
	*获取商品列表
	* /app/v2/goods_seckill/getList
	* 需要登录 
	* @is_need_login true
	* @method all
	*/
	public function getList($status='',$pageIndex=1,$pageSize=20){
		try {
			$where[] = ['shop_id', '=', request()->shopId];
			$time = date("Y-m-d H:i:s");	
	    	if($status == 1){
				$where[] = ['start_time', '<', $time];
				$where[] = ['stop_time', '>', $time];
			}
			if($status == 2) $where[] = ['start_time', '>', $time];
			if($status == 3) $where[] = ['stop_time', '<', $time];
			$result = model('GoodsSeckill')->getSeckillList($where,$pageIndex,$pageSize);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 添加
	* /app/v2/goods_seckill/add
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function add(){
		$request =  request();
		$param = $request ->post();
		$param['shop_id'] = $request ->shopInfo['id'];
		try {
			$db_goods_seckill = model('GoodsSeckill');
			$result = $db_goods_seckill->addSeckill($param);
			if(!$result){
				return $this->ReError($db_goods_seckill->errorInfo);
			}
			return $this->ReSucess($result,'申请成功，等待审核！');	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 编辑
	* /app/v2/goods_seckill/edit
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function edit($id){
		$request =  request();
		$param = $request ->post();
		$param['shop_id'] = $request ->shopInfo['id'];
		try {
			$db_goods_seckill = model('GoodsSeckill');
			$result = $db_goods_seckill->editSeckill($param);
			if(!$result){
				return $this->ReError($db_goods_seckill->errorInfo);
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	*删除
	* /app/v2/goods_seckill/del
	* 需要登录 
	* @is_need_login true
	*@method post 
	*/
	public function del($id){
		if( !trim($id) ){
			return $this->ReError('未找到对应id参数!');
		}
		try {
			$db_goods_seckill = model('GoodsSeckill');
			$result = $db_goods_seckill->remove($id);
			if(!$result){
				return $this->ReError($db_goods_seckill->errorInfo);
			}
			return $this->ReSucess();	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}



